﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CuitWinControl.cs" company="">
//   
// </copyright>
// <summary>
//   The cuit win control.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.CUIT.CuitControls.WinControls
{
    using System;

    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UITesting.WinControls;

    using UITest.CUIT.CuitControls.Exceptions;
    using UITest.CUIT.CuitControls.Extension;

    /// <summary>
    ///     The cuit win control.
    /// </summary>
    public class CuitWinControl : CuitControlBase<WinControl>
    {
    }

    /// <summary>
    /// The cuit win control.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class CuitWinControl<T> : CuitControlBase<T>, ICuitWinControl
        where T : WinControl
    {
        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="CuitWinControl{T}" /> class.
        /// </summary>
        public CuitWinControl()
            : this(new UITestControl(), string.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinControl{T}"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        public CuitWinControl(UITestControl testControl)
            : this(testControl, string.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinControl{T}"/> class.
        /// </summary>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitWinControl(string searchProperties)
        {
            this.searchPropertiesInString = searchProperties;
            this.CreateControls(null, searchProperties);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinControl{T}"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitWinControl(UITestControl testControl, string searchProperties)
        {
            this.searchPropertiesInString = searchProperties;
            this.CreateControls(testControl, searchProperties);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinControl{T}"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitWinControl(ICuitControlBase wrappedControl, string searchProperties)
            : this(wrappedControl.GetMicrosoftTestControl(), searchProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinControl{T}"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        public CuitWinControl(ICuitControlBase wrappedControl, string searchProperties, string filterProperties)
            : this(wrappedControl.GetMicrosoftTestControl(), searchProperties, filterProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinControl{T}"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        public CuitWinControl(UITestControl testControl, string searchProperties, string filterProperties)
        {
            this.searchPropertiesInString = searchProperties;
            this.filterPropertiesInString = filterProperties;
            this.CreateControls(testControl, searchProperties);
            this.SetFilterProperties(filterProperties);
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets the help text.
        /// </summary>
        public string HelpText
        {
            get
            {
                return this._control.HelpText;
            }
        }

        /// <summary>
        ///     Gets the parent.
        /// </summary>
        /// <exception cref="CuitInvalidTraversal">
        /// </exception>
        public override ICuitControlBase Parent
        {
            get
            {
                this._control.WaitForControlReady();
                ICuitControlBase ret = null;
                try
                {
                    ret = this.WrapUtil((WinControl)this._control.GetParent());
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new CuitInvalidTraversal(string.Format("({0}).Parent", this._control.GetType().Name));
                }

                return ret;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create controls.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// </exception>
        public T CreateControls(UITestControl testControl, string searchProperties)
        {
            object winControl;

            if (typeof(T) == typeof(WinButton))
            {
                winControl = new WinButton(testControl);
            }
            else if (typeof(T) == typeof(WinCalendar))
            {
                winControl = new WinCalendar(testControl);
            }
            else if (typeof(T) == typeof(WinCell))
            {
                winControl = new WinCell(testControl);
            }
            else if (typeof(T) == typeof(WinCheckBox))
            {
                winControl = new WinCheckBox(testControl);
            }
            else if (typeof(T) == typeof(WinCheckBoxTreeItem))
            {
                winControl = new WinCheckBoxTreeItem(testControl);
            }
            else if (typeof(T) == typeof(WinClient))
            {
                winControl = new WinClient(testControl);
            }
            else if (typeof(T) == typeof(WinColumnHeader))
            {
                winControl = new WinColumnHeader(testControl);
            }
            else if (typeof(T) == typeof(WinComboBox))
            {
                winControl = new WinComboBox(testControl);
            }
            else if (typeof(T) == typeof(WinCustom))
            {
                winControl = new WinCustom(testControl);
            }
            else if (typeof(T) == typeof(WinDateTimePicker))
            {
                winControl = new WinDateTimePicker(testControl);
            }
            else if (typeof(T) == typeof(WinEdit))
            {
                winControl = new WinEdit(testControl);
            }
            else if (typeof(T) == typeof(WinGroup))
            {
                winControl = new WinGroup(testControl);
            }
            else if (typeof(T) == typeof(WinHyperlink))
            {
                winControl = new WinHyperlink(testControl);
            }
            else if (typeof(T) == typeof(WinList))
            {
                winControl = new WinList(testControl);
            }
            else if (typeof(T) == typeof(WinListItem))
            {
                winControl = new WinListItem(testControl);
            }
            else if (typeof(T) == typeof(WinMenu))
            {
                winControl = new WinMenu(testControl);
            }
            else if (typeof(T) == typeof(WinMenuBar))
            {
                winControl = new WinMenuBar(testControl);
            }
            else if (typeof(T) == typeof(WinMenuItem))
            {
                winControl = new WinMenuItem(testControl);
            }
            else if (typeof(T) == typeof(WinPane))
            {
                winControl = new WinPane(testControl);
            }
            else if (typeof(T) == typeof(WinProgressBar))
            {
                winControl = new WinProgressBar(testControl);
            }
            else if (typeof(T) == typeof(WinRadioButton))
            {
                winControl = new WinRadioButton(testControl);
            }
            else if (typeof(T) == typeof(WinRow))
            {
                winControl = new WinRow(testControl);
            }
            else if (typeof(T) == typeof(WinRowHeader))
            {
                winControl = new WinRowHeader(testControl);
            }
            else if (typeof(T) == typeof(WinScrollBar))
            {
                winControl = new WinScrollBar(testControl);
            }
            else if (typeof(T) == typeof(WinSeparator))
            {
                winControl = new WinSeparator(testControl);
            }
            else if (typeof(T) == typeof(WinSlider))
            {
                winControl = new WinSlider(testControl);
            }
            else if (typeof(T) == typeof(WinSpinner))
            {
                winControl = new WinSpinner(testControl);
            }
            else if (typeof(T) == typeof(WinSplitButton))
            {
                winControl = new WinSplitButton(testControl);
            }
            else if (typeof(T) == typeof(WinStatusBar))
            {
                winControl = new WinStatusBar(testControl);
            }
            else if (typeof(T) == typeof(WinTable))
            {
                winControl = new WinTable(testControl);
            }
            else if (typeof(T) == typeof(WinTabList))
            {
                winControl = new WinTabList(testControl);
            }
            else if (typeof(T) == typeof(WinTabPage))
            {
                winControl = new WinTabPage(testControl);
            }
            else if (typeof(T) == typeof(WinText))
            {
                winControl = new WinText(testControl);
            }
            else if (typeof(T) == typeof(WinTitleBar))
            {
                winControl = new WinTitleBar(testControl);
            }
            else if (typeof(T) == typeof(WinToolBar))
            {
                winControl = new WinToolBar(testControl);
            }
            else if (typeof(T) == typeof(WinToolTip))
            {
                winControl = new WinToolTip(testControl);
            }
            else if (typeof(T) == typeof(WinTree))
            {
                winControl = new WinTree(testControl);
            }
            else if (typeof(T) == typeof(WinTreeItem))
            {
                winControl = new WinTreeItem(testControl);
            }
            else if (typeof(T) == typeof(WinWindow))
            {
                winControl = new WinWindow(testControl);
            }
            else
            {
                throw new Exception(string.Format("CreateControls: '{0}' not supported", typeof(T).Name));
            }

            // Casting the Object to microsoft Control
            this._control = (T)winControl;
            this._control.CuitSetSearchProperties<T>(searchProperties);

            return this._control;
        }

        /// <summary>
        /// The set filter properties.
        /// </summary>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public T SetFilterProperties(string filterProperties)
        {
            this._control.CuitSetFilterProperties<T>(filterProperties);
            return this._control;
        }

        #endregion

        #region Methods

        private ICuitControlBase WrapUtil(WinControl control)
        {
            ICuitControlBase _con = null;

            if (control.GetType() == typeof(WinButton))
            {
                _con = new CuitWinButton();
            }
            else if (control.GetType() == typeof(WinCalendar))
            {
                _con = new CuitWinCalendar();
            }
            else if (control.GetType() == typeof(WinCell))
            {
                _con = new CuitWinCell();
            }
            else if (control.GetType() == typeof(WinCheckBox))
            {
                _con = new CuitWinCheckBox();
            }
            else if (control.GetType() == typeof(WinCheckBoxTreeItem))
            {
                _con = new CuitWinCheckBoxTreeItem();
            }
            else if (control.GetType() == typeof(WinClient))
            {
                _con = new CuitWinClient();
            }
            else if (control.GetType() == typeof(WinColumnHeader))
            {
                _con = new CuitWinColumnHeader();
            }
            else if (control.GetType() == typeof(WinComboBox))
            {
                _con = new CuitWinComboBox();
            }
            else if (control.GetType() == typeof(WinCustom))
            {
                _con = new CuitWinCustom();
            }
            else if (control.GetType() == typeof(WinDateTimePicker))
            {
                _con = new CuitWinDateTimePicker();
            }
            else if (control.GetType() == typeof(WinEdit))
            {
                _con = new CuitWinEdit();
            }
            else if (control.GetType() == typeof(WinGroup))
            {
                _con = new CuitWinGroup();
            }
            else if (control.GetType() == typeof(WinHyperlink))
            {
                _con = new CuitWinHyperlink();
            }
            else if (control.GetType() == typeof(WinList))
            {
                _con = new CuitWinList();
            }
            else if (control.GetType() == typeof(WinListItem))
            {
                _con = new CuitWinListItem();
            }
            else if (control.GetType() == typeof(WinMenu))
            {
                _con = new CuitWinMenu();
            }
            else if (control.GetType() == typeof(WinMenuBar))
            {
                _con = new CuitWinMenuBar();
            }
            else if (control.GetType() == typeof(WinMenuItem))
            {
                _con = new CuitWinMenuItem();
            }
            else if (control.GetType() == typeof(WinPane))
            {
                _con = new CuitWinPane();
            }
            else if (control.GetType() == typeof(WinProgressBar))
            {
                _con = new CuitWinProgressBar();
            }
            else if (control.GetType() == typeof(WinRadioButton))
            {
                _con = new CuitWinRadioButton();
            }
            else if (control.GetType() == typeof(WinRow))
            {
                _con = new CuitWinRow();
            }
            else if (control.GetType() == typeof(WinRowHeader))
            {
                _con = new CuitWinRowHeader();
            }
            else if (control.GetType() == typeof(WinScrollBar))
            {
                _con = new CuitWinScrollBar();
            }
            else if (control.GetType() == typeof(WinSeparator))
            {
                _con = new CuitWinSeparator();
            }
            else if (control.GetType() == typeof(WinSlider))
            {
                _con = new CuitWinSlider();
            }
            else if (control.GetType() == typeof(WinSpinner))
            {
                _con = new CuitWinSpinner();
            }
            else if (control.GetType() == typeof(WinSplitButton))
            {
                _con = new CuitWinSplitButton();
            }
            else if (control.GetType() == typeof(WinStatusBar))
            {
                _con = new CuitWinStatusBar();
            }
            else if (control.GetType() == typeof(WinTable))
            {
                _con = new CuitWinTable();
            }
            else if (control.GetType() == typeof(WinTabList))
            {
                _con = new CuitWinTabList();
            }
            else if (control.GetType() == typeof(WinTabPage))
            {
                _con = new CuitWinTabPage();
            }
            else if (control.GetType() == typeof(WinText))
            {
                _con = new CuitWinText();
            }
            else if (control.GetType() == typeof(WinTitleBar))
            {
                _con = new CuitWinTitleBar();
            }
            else if (control.GetType() == typeof(WinToolBar))
            {
                _con = new CuitWinToolBar();
            }
            else if (control.GetType() == typeof(WinToolTip))
            {
                _con = new CuitWinToolTip();
            }
            else if (control.GetType() == typeof(WinTree))
            {
                _con = new CuitWinTree();
            }
            else if (control.GetType() == typeof(WinTreeItem))
            {
                _con = new CuitWinTreeItem();
            }
            else if (control.GetType() == typeof(WinWindow))
            {
                _con = new CuitWinWindow();
            }
            else
            {
                throw new Exception(string.Format("WrapUtil: '{0}' not supported", control.GetType().Name));
            }

            _con.WrapReady(control);

            return _con;
        }

        #endregion
    }
}